#pragma once
#include <iostream>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>

namespace Sock
{
    const int defaultbacklog = 10;
    class Sock
    {
    public:
        Sock(int port, std::string ip)
            : _port(port), _ip(ip), _backlog(defaultbacklog)
        {
        }

        void InitSev()
        {
            if (!Socket())
                return;
            if (!Bind())
                return;
            if (!Listen())
                return;
            ReuseAddress();
            std::cout << "Init Socket Success" << std::endl;
        }

        void InitCli()
        {
            if (!Socket())
                return;
            if (!Connect())
                return;
            std::cout << "Init Socket Success" << std::endl;
        }

        int Accept(std::string &ip, int &port)
        {
            struct sockaddr_in peer;
            socklen_t len = sizeof(peer);
            int connfd = accept(_fd, (struct sockaddr *)&peer, &len);
            if (connfd < 0)
            {
                std::cout << "accept error" << std::endl;
                return -1;
            }
            port = ntohs(peer.sin_port);
            ip = inet_ntoa(peer.sin_addr);
            std::cout << "connfd: " << connfd << " client ip:" << inet_ntoa(peer.sin_addr) << std::endl;
            return connfd;
        }

        int get_sockfd()
        {
            return _fd;
        }

        ~Sock()
        {
            close(_fd);
        }

    private:
        bool Socket()
        {
            _fd = socket(AF_INET, SOCK_STREAM, 0);
            if (_fd < 0)
            {
                std::cout << "socket error" << std::endl;
                return false;
            }
            return true;
        }
        bool Bind()
        {
            struct sockaddr_in local;
            local.sin_family = AF_INET;
            local.sin_port = htons(_port);
            local.sin_addr.s_addr = inet_addr(_ip.c_str());
            if (bind(_fd, (struct sockaddr *)&local, sizeof(local)) < 0)
            {
                std::cout << "bind error" << std::endl;
                return false;
            }
            return true;
        }

        bool Listen()
        {
            if (listen(_fd, _backlog) < 0)
            {
                std::cout << "listen error" << std::endl;
                return false;
            }
            return true;
        }

        bool Connect()
        {
            struct sockaddr_in peer;
            peer.sin_family = AF_INET;
            peer.sin_port = htons(_port);
            peer.sin_addr.s_addr = inet_addr(_ip.c_str());
            if (connect(_fd, (struct sockaddr *)&peer, sizeof(peer)) < 0)
            {
                std::cout << "connect error" << std::endl;
                return false;
            }
            return true;
        }

        // 设置套接字选项---开启地址端⼝重⽤
        void ReuseAddress()
        {
            // int setsockopt(int fd, int leve, int optname, void *val, intvallen)
            int val = 1;
            // SO_REUSEADDR允许一个套接字绑定到一个已经被另一个套接字使用的地址上，只要那个套接字不在监听状态。
            setsockopt(_fd, SOL_SOCKET, SO_REUSEADDR, (void *)&val,
                       sizeof(int));
            val = 1;
            // SO_REUSEPORT允许多个套接字绑定到同一个端口上，每个套接字都有其独立的接收队列。
            setsockopt(_fd, SOL_SOCKET, SO_REUSEPORT, (void *)&val,
                       sizeof(int));
        }

    private:
        int _fd;
        int _port;
        std::string _ip;
        int _backlog;
    };

}
